const { spawn } = require("child_process")

const child = spawn("pnpm", ["exec", "plasmo", "dev"], { shell: true })

let printed = false
const detect = (chunk) => {
  const text = chunk.toString()
  if (!printed) {
    const m = text.match(/https?:\/\/(?:localhost|127\.0\.0\.1):(\d+)/i)
    if (m && m[1]) {
      printed = true
      console.log(`Dev server port: ${m[1]}`)
    }
  }
  process.stdout.write(text)
}

child.stdout.on("data", detect)
child.stderr.on("data", (c) => process.stderr.write(c))
child.on("close", (code) => process.exit(code))
